package com.wnxy.carl.carlorderassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.carl.carlorderassembly.entity.BusinessFlow;
import com.wnxy.carl.carlorderassembly.mapper.BusinessFlowMapper;
import com.wnxy.carl.carlorderassembly.service.IBusinessFlowService;
import com.wnxy.carl.dto.BusinessFlowDTO;
import com.wnxy.carl.dto.PageDTO;
import com.wnxy.carl.exception.DemoRuntimeException;
import com.wnxy.carl.param.IncreaseBusinessFlowParam;
import com.wnxy.carl.param.PageParam;
import com.wnxy.carl.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 业务订单执行步骤表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-11-12
 */
@Service
@Slf4j
public class BusinessFlowServiceImpl extends ServiceImpl<BusinessFlowMapper, BusinessFlow> implements IBusinessFlowService {

    @Resource
    private BusinessFlowMapper businessFlowMapper;

    //查看业务流程 PageDTO<BusinessFlowDTO>
    public PageDTO<BusinessFlowDTO> seeBusinessFlow(PageParam<Integer> pageParam) {
        log.info("开始执行查看业务流程功能：参数={}",pageParam);
        IPage<BusinessFlow> page = new Page<>(pageParam.getPageIndex(), pageParam.getPageSize());
        QueryWrapper<BusinessFlow> qw = new QueryWrapper<BusinessFlow>();
        qw.eq("order_id", pageParam.getData());
        IPage<BusinessFlow> businessFlowIPage = businessFlowMapper.selectPage(page, qw);
        PageDTO<BusinessFlowDTO> pageDTO = PageDTO.bulid((Page<?>) businessFlowIPage, BusinessFlowDTO.class);
        return pageDTO;
    }
    //修改业务流程 BusinessFlowDTO
    public BusinessFlowDTO amendBusinessFlow(Integer flowId, Integer flowStatus) {
        log.info("开始执行查看业务流程功能：参数={},{}",flowId,flowStatus);
        BusinessFlow businessFlow = businessFlowMapper.selectById(flowId);
        if (null == businessFlow){
            throw new DemoRuntimeException("每有这个业务流程");
        }
        UpdateWrapper<BusinessFlow> uw = new UpdateWrapper<>();
        uw.set("flow_status",flowStatus);
        uw.eq("flow_id",flowId);
        int update = businessFlowMapper.update(null, uw);
        if (update == 0){
            throw new DemoRuntimeException("修改失败");
        }
        businessFlow.setFlowStatus(flowStatus);
        BusinessFlowDTO businessFlowDTO = new BusinessFlowDTO();
        BeanUtils.copyProperties(businessFlow,businessFlowDTO);
        return businessFlowDTO;
    }
    //删除业务流程 Boolean
    public void cancelBusinessFlow(Integer flowId) {
        log.info("开始执行删除业务流程功能：参数={}",flowId);
        BusinessFlow businessFlow = businessFlowMapper.selectById(flowId);
        if (null == businessFlow){
            throw new DemoRuntimeException("每有这个业务流程");
        }
        int i = businessFlowMapper.deleteById(flowId);
        if (i == 0){
            throw new DemoRuntimeException("删除失败");
        }
    }
    //添加业务流程 BusinessFlowDTO
    public BusinessFlowDTO increaseBusinessFlow(IncreaseBusinessFlowParam businessFlowParam) {
        log.info("添加业务流程功能：参数={}",businessFlowParam);
        BusinessFlow businessFlow = new BusinessFlow();
        BeanUtils.copyProperties(businessFlowParam,businessFlow);
        int insert = businessFlowMapper.insert(businessFlow);
        if (insert == 0){
            throw new DemoRuntimeException("添加失败");
        }
        BusinessFlow businessFlow1 = businessFlowMapper.selectById(businessFlow.getFlowId());
        BusinessFlowDTO businessFlowDTO = new BusinessFlowDTO();
        BeanUtils.copyProperties(businessFlow1,businessFlowDTO);
        return businessFlowDTO;
    }

}
